package com.svo.weitu.activity.fragment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.svo.weitu.R;
import com.svo.weitu.activity.MainActivity;

public class NieFragment extends Fragment implements OnClickListener{
	private View root;
//	private TextView titleTv;
	private Button btn_take_photo, btn_pick_photo, btn_cancel;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.fragment_nie, null);
		root.findViewById(R.id.menu).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (getActivity() instanceof MainActivity) {
					MainActivity mainActivity = (MainActivity) getActivity();
					mainActivity.showMenu();
				}
			}
		});
		root.findViewById(R.id.order).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((MainActivity)getActivity()).showSecondaryMenu();
			}
		});
//		titleTv = (TextView) root.findViewById(R.id.title);
		btn_take_photo = (Button) root.findViewById(R.id.btn_take_photo);
		btn_pick_photo = (Button) root.findViewById(R.id.btn_pick_photo);
		btn_cancel = (Button) root.findViewById(R.id.btn_cancel);

		// 添加按钮监听
		btn_cancel.setOnClickListener(this);
		btn_pick_photo.setOnClickListener(this);
		btn_take_photo.setOnClickListener(this);
		return root;
	}

	/**
	 * 保存图片
	 * @param imageView
	 * @param path 保存目录
	 * @param picName 图片名字
	 * @return 保存的文件对象;保存失败返回null
	 */
	public static File savePic(Bitmap bitmap,String path,String picName){
		File dir = new File(path);
		if (dir != null && !dir.exists()) {
			dir.mkdirs();
		}
		File file = new File(dir, picName);
		if (file != null && file.exists()) {
			return file;
		}
		try {
			FileOutputStream fos = new FileOutputStream(file);
			bitmap.compress(CompressFormat.PNG, 100, fos);
			fos.close();
		}catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return file;
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_take_photo:
			try {
				//拍照我们用Action为MediaStore.ACTION_IMAGE_CAPTURE，
				//有些人使用其他的Action但我发现在有些机子中会出问题，所以优先选择这个
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, Activity.RESULT_FIRST_USER);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case R.id.btn_pick_photo:
			try {
				//选择照片的时候也一样，我们用Action为Intent.ACTION_GET_CONTENT，
				//有些人使用其他的Action但我发现在有些机子中会出问题，所以优先选择这个
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(intent, Activity.RESULT_FIRST_USER);
			} catch (ActivityNotFoundException e) {

			}
			break;
		case R.id.btn_cancel:
			MainActivity mainActivity = (MainActivity) getActivity();
			mainActivity.showMenu();
			break;
		default:
			break;
		}

	}
}
